package com.chat.common.common.event.Listener;

import com.chat.common.chat.domain.entity.Message;
import com.chat.common.chat.service.cache.MsgCache;
import com.chat.common.common.event.MessageRecallEvent;
import com.chat.common.user.service.impl.PushService;
import com.chat.common.websocket.service.adapter.WebSocketAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.awt.*;

@Slf4j
@Component
public class MessageRecallListener {
    @Resource
    private MsgCache msgCache;
    @Resource
    private PushService pushService;

    @Async
    @TransactionalEventListener(classes = MessageRecallEvent.class,fallbackExecution = true)
    public void evictMsg(MessageRecallEvent event){
        msgCache.evictMsg(event.getRecallDTO().getMsgId());
    }

    @Async
    @TransactionalEventListener(classes = MessageRecallEvent.class,fallbackExecution = true)
    public void sendAllMsg(MessageRecallEvent event){
        pushService.sendPushMsg(WebSocketAdapter.buildMsgRecall(event.getRecallDTO()));
    }



}
